﻿using System.ComponentModel;
using System.Windows.Forms;
using CDIO.Util;
using CDIO.ICCard;
using 餐厅模块;

namespace 图书馆模块
{
    public partial class Library : Form
    {
        private const string KeyName = "图书馆模块序号";
        private const string SNKey = "ServerName";
        private const string DNKey = "DatabaseName";
        private const string UNKey = "UserName";
        private const string PSWKey = "Password";
        private string ServerName;
        private string DatabaseName;
        private string UserName;
        private string Password;

        private string tBarcode;


        public Library()
        {
            InitializeComponent();
            int index = (int)RegisterOprate.ReadRegData(KeyName);
            ServerName = "127.0.0.1";
            DatabaseName = "schoolcard";
            UserName = "card";
            Password = "card";
            if (ReadRegConfig() < 4)
            {
                Configure();
            }
            tabs.SelectedIndex = index;
            EnableTimer();
            LoanList.Items.Add("已借的书");
            tBarcode = "";
        }

        private int ReadRegConfig()
        {
            int readed = 0;
            string regval = RegisterOprate.ReadRegData(SNKey) as string;
            if (regval != null)
            {
                ServerName = regval;
                readed++;
            }
            regval = RegisterOprate.ReadRegData(DNKey) as string;
            if (regval != null)
            {
                DatabaseName = regval;
                readed++;
            }
            regval = RegisterOprate.ReadRegData(UNKey) as string;
            if (regval != null)
            {
                UserName = regval;
                readed++;
            }
            regval = RegisterOprate.ReadRegData(PSWKey) as string;
            if (regval != null)
            {
                Password = regval;
                readed++;
            }
            return readed;
        }

        private void SaveRegConfig()
        {
            RegisterOprate.SaveRegData(SNKey, ServerName);
            RegisterOprate.SaveRegData(DNKey, DatabaseName);
            RegisterOprate.SaveRegData(UNKey, UserName);
            RegisterOprate.SaveRegData(PSWKey, Password);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            RegisterOprate.SaveRegData(KeyName, tabs.SelectedIndex);
            if (MessageBox.Show("确认退出?", "请确认", MessageBoxButtons.YesNo) == DialogResult.No)
                e.Cancel = true;
            base.OnClosing(e);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (tabs.SelectedIndex != 2)
            {
                if (keyData >= Keys.D0 && keyData <= Keys.D9)
                {
                    tBarcode += keyData - Keys.D0;
                    return true;
                }
                if (keyData == System.Windows.Forms.Keys.Return)
                {
                    MessageBox.Show(tBarcode);
                    tBarcode = "";
                    return true;
                }
                return base.ProcessDialogKey(keyData);
            }
            return base.ProcessDialogKey(keyData);
        }

        private void FeeDetail_Click(object sender, System.EventArgs e)
        {
        }
        private void Configure()
        {
            ConfigDialog c = new ConfigDialog();
            c.servername.Text = ServerName;
            c.databasename.Text = DatabaseName;
            c.username.Text = UserName;
            c.password.Text = Password;
            if (DialogResult.OK == c.ShowDialog(this))
            {
                ServerName = c.servername.Text;
                DatabaseName = c.databasename.Text;
                UserName = c.username.Text;
                Password = c.password.Text;
            }

        }

        private void RefreshCard()
        {
            string ReadStuName;
            ushort ReadCardNo;
            long ReadStuNo;
            ushort ReadBlance;
            float ReadBalnceF;
            Card.ReadCard(out ReadCardNo, out ReadStuNo, out ReadStuName, out ReadBlance);
            ReadBalnceF = ReadBlance / 10.0f;
            this.StuName.Text = ReadStuName;
            this.StuNo.Text = ReadStuNo.ToString("D9");
            this.CardNo.Text = ReadCardNo.ToString("D5");

        }

        private uint PreCardSerial;
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Configure();
        }

        private void CardDetectTimer_Tick(object sender, System.EventArgs e)
        {
            uint CurCardSerial = Card.DetectedCard;
            if (CurCardSerial != 0)
            {
                if (CurCardSerial != PreCardSerial)
                    RefreshCard();
            }
            PreCardSerial = CurCardSerial;
        }

        private void EnableTimer()
        {
            if (tabs.SelectedIndex == 0)
            {
                CardDetectTimer.Enabled = true;
            }
            else
            {
                CardDetectTimer.Enabled = false;
            }
        }

        private void tabs_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            EnableTimer();
        }

        private void AddBookItem(string bookTitle,string author,string bookPress,string publishDate,string bookOutline,ulong bookBarcode,string isbnCode)
        {

        }

        private void AddBook_Click(object sender, System.EventArgs e)
        {

        }

        private void ResetBook_Click(object sender, System.EventArgs e)
        {
            bookauthor.Text="";
            bookbarcode.Text="";
            bookISBN.Text="";
            publisdate.Text="";
            press.Text="";
            bookname.Text="";
            outline.Text="";
        }

    }
}
